﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentCreaturecollection : Component, IUpdateable
    {
        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain subsystemTerrain;

        public SubsystemPickables subsystemPickables;

        public ComponentCreature componentCreature;

        private ComponentPathfinding m_componentPathfinding;

        public ComponentMiner m_componentMiner;

        public SubsystemTimeOfDay m_subsystemTimeOfDay;

        public bool Active;

        public CollectionType m_type;

        public int[] HandledBlocks => new int[9] { 28, 99, 131, 244, 132, 174, 204, 310, 311 };

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            base.Load(valuesDictionary, idToEntityMap);
            m_componentMiner = base.Entity.FindComponent<ComponentMiner>(throwOnError: true);
            subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            subsystemPickables = base.Project.FindSubsystem<SubsystemPickables>(throwOnError: true);
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTimeOfDay = base.Project.FindSubsystem<SubsystemTimeOfDay>(throwOnError: true);
            componentCreature = base.Entity.FindComponent<ComponentCreature>();
            m_componentPathfinding = base.Entity.FindComponent<ComponentPathfinding>(throwOnError: true);
            Active = valuesDictionary.GetValue<bool>("Active");
            m_type = valuesDictionary.GetValue<CollectionType>("CollectionType");
        }

        public void Update(float dt)
        {
            IInventory inventory = m_componentMiner.Inventory;
            ComponentInventory componentInventory = componentCreature.ComponentBody.Entity.FindComponent<ComponentInventory>();
            ComponentChaseBehavior componentChaseBehavior = componentCreature.ComponentBody.Entity.FindComponent<ComponentChaseBehavior>();
            if (!Active)
            {
                return;
            }

            foreach (Pickable pickable in subsystemPickables.Pickables)
            {
                TerrainChunk chunkAtCell = subsystemTerrain.Terrain.GetChunkAtCell(Terrain.ToCell(pickable.Position.X), Terrain.ToCell(pickable.Position.Z));
                if (componentInventory == null || chunkAtCell == null || pickable.FlyToPosition.HasValue || !(componentCreature.ComponentHealth.Health > 0f) || componentChaseBehavior.Target != null)
                {
                    continue;
                }

                Vector3 vector = componentCreature.ComponentBody.Position + new Vector3(0f, 0.8f, 0f);
                float num = (vector - pickable.Position).LengthSquared();
                if (num < 10f)
                {
                    switch (m_type)
                    {
                        case CollectionType.Normal:
                            {
                                for (int k = 0; k < inventory.SlotsCount; k++)
                                {
                                    int num3 = ComponentInventoryBase.FindAcquireSlotForItem(inventory, pickable.Value);
                                    if (num3 < 0)
                                    {
                                        break;
                                    }

                                    if (num3 >= 0)
                                    {
                                        m_componentPathfinding.SetDestination(pickable.Position, 3f, 3.75f, 20, useRandomMovements: true, ignoreHeightDifference: false, raycastDestination: false, null);
                                    }
                                }

                                break;
                            }
                        case CollectionType.Grower:
                            {
                                for (int i = 0; i < inventory.SlotsCount; i++)
                                {
                                    int num2 = Terrain.ExtractContents(pickable.Value);
                                    if (num2 < 0)
                                    {
                                        break;
                                    }

                                    if (num2 < 0)
                                    {
                                        continue;
                                    }

                                    for (int j = 0; j < HandledBlocks.Length; j++)
                                    {
                                        if (HandledBlocks[j] == num2)
                                        {
                                            m_componentPathfinding.SetDestination(pickable.Position, 3f, 3.75f, 20, useRandomMovements: true, ignoreHeightDifference: false, raycastDestination: false, null);
                                        }
                                    }
                                }

                                break;
                            }
                    }
                }

                if (!(num < 4f) || m_type != 0)
                {
                    continue;
                }

                for (int l = 0; l < inventory.SlotsCount; l++)
                {
                    int num4 = ComponentInventoryBase.FindAcquireSlotForItem(inventory, pickable.Value);
                    if (num4 < 0)
                    {
                        break;
                    }

                    if (num4 >= 0)
                    {
                        pickable.ToRemove = true;
                        pickable.FlyToPosition = vector;
                        pickable.Count = ComponentInventoryBase.AcquireItems(componentInventory, pickable.Value, pickable.Count);
                    }
                }
            }
        }
    }
}